package com.subject.common.entity;

import com.subject.common.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private int code;
    private String message;
    private T data;


    /**
     * 用于get请求获取资源
     * */
    public static <T> Result<T> getSuccess(T data) {
        return new Result<>(200, "获取成功", data);
    }
    public static <T> Result<T> getSuccess(T data, String message) {
        return new Result<>(200, message, data);
    }

    /**
     * 用于post添加新的资源
     * */
    public static <T> Result<T> addSuccess(String message) {
        return new Result<>(200, message, null);
    }
    public static <T> Result<T> addSuccess() {
        return new Result<>(200, "添加成功", null);
    }

    /**
     * 用于post修改新的资源
     * */
    public static <T> Result<T> updateSuccess() {
        return new Result<>(200, "修改成功", null);
    }
    public static <T> Result<T> updateSuccess(String message) {
        return new Result<>(200, message, null);
    }

    /**
     * 用于post删除的资源
     * */
    public static <T> Result<T> deleteSuccess() {
        return new Result<>(200, "删除成功", null);
    }
    public static <T> Result<T> deleteSuccess(String message) {
        return new Result<>(200, message, null);
    }

    /**
     * 根据状态码获取失败信息
     */
    public static <T> Result<T> fail(int code) {
        return new Result<>(code, Objects.requireNonNull(ResultCodeEnum.getResultCodeEnum(code)).getMsg(), null);
    }
    /**
     * 自定义的返回状态信息，输入code和message返回自定义的状态信息
     * */
    public static <T> Result<T> fail(int code, String message) {
        return new Result<>(code, message, null);
    }
}
